#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QTouchEvent>
#include <QDebug>
#include <QPainter>
#include <QPen>
#include <QHostAddress>
#include <QNetworkInterface>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QMainWindow::setAttribute(Qt::WA_AcceptTouchEvents);
    QMainWindow::setMouseTracking(true);

    tcpServer = NULL;
    tcpSocket = NULL;
    isOpen = false;
    canPress = false;
    showNet = false;
    netStep = 50;
    startX = 80;

    QPalette pal =ui->btn->palette();
    pal.setColor(QPalette::Button,Qt::darkGray);
    ui->btn->setPalette(pal);
    ui->btn->setAutoFillBackground(true);
    ui->btn->setFlat(true);
    ui->btn->setVisible(false);

    pal =ui->labelCur->palette();
    pal.setColor(QPalette::Window,Qt::darkGray);
    ui->labelCur->setPalette(pal);
    ui->labelCur->setAutoFillBackground(true);
    ui->labelCur->setVisible(true);

    for(int i=0;i<80;i++)
    {
        QLabel* label = new QLabel(this);
        label->setGeometry(10,i*20,100,2);
        label->setVisible(false);
        QPalette pal =label->palette();
        pal.setColor(QPalette::Window,Qt::lightGray);
        label->setPalette(pal);
        label->setAutoFillBackground(true);
        net.push_back(label);
    }

    QList<QHostAddress> addr = QNetworkInterface::allAddresses();
    ui->show->setText(addr.at(0).toString());
    ui->show2->setText(addr.at(1).toString());


    tcpServer = new QTcpServer(this);
    tcpServer->listen(QHostAddress::Any,8111);

    connect(tcpServer,&QTcpServer::newConnection,[=](){
        //取出建立好连接的的套接字
        tcpSocket = tcpServer->nextPendingConnection();

        //获取对方的IP和端口
        QString ip = tcpSocket->peerAddress().toString();
        qint16  port = tcpSocket->peerPort();
        QString temp = QString("Client: [%1:%2]").arg(ip).arg(port);
        ui->show->setText(temp);
        isOpen = true;

        char buf[64];
        sprintf(buf,"information %d %d", this->width(),this->height()-startX);
        send_str(buf);

        ui->show3->setText(QString::number(this->width())+tr(" ")+QString::number(this->height()));

        connect(tcpSocket, &QTcpSocket::readyRead,[=](){
             QByteArray buf = tcpSocket->readAll();
             cout <<buf.data()<<endl;
         });

    });
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::showLabel(int step)
{
    int showed=0;
    for(int x=0;x<=this->width();x+=step)
    {
        QLabel *label = net[showed];
        label->setVisible(true);
        label->setGeometry(x,startX,2,this->height());
        showed++;
    }

    for(int y=startX;y<=this->height();y+=step)
    {
        QLabel *label = net[showed];
        label->setVisible(true);
        label->setGeometry(0,y,this->width(),2);
        showed++;
    }

    for(showed;showed<net.size();showed++)
    {
        QLabel *label = net[showed];
        label->setVisible(false);
    }

    char buf[64];
    sprintf(buf,"step %d %d", this->width()/step, this->height()/step);
    send_str(buf);

}


void MainWindow::paintEvent(QPaintEvent *event)
{

}

void MainWindow::drawPoint(int x, int y)
{
    this->x=x;
    this->y=y;
    if(canPress)
        ui->btn->setGeometry(x-40,y-40,80,80);
    else
        ui->labelCur->setGeometry(x-25,y-25,50,50);

}

void MainWindow::judgeEvent(QEvent *event)
{

}

bool MainWindow::event(QEvent *event)
{
    switch (event->type()) {
    case QEvent::MouseMove:{
        QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event);
        ui->show->setText(tr("MouseMove:")+QString::number(mouseEvent->pos().x())+tr(" ")+QString::number(mouseEvent->pos().y()));
        char buf[64];
        sprintf(buf,"mouseMove %d %d", mouseEvent->pos().x(), mouseEvent->pos().y()-startX);
        send_str(buf);

        drawPoint(mouseEvent->pos().x(), mouseEvent->pos().y());
        break;
    }
    case QEvent::MouseButtonPress:{
        QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event);
        ui->show2->setText(tr("MouseButtonPress:")+QString::number(mouseEvent->pos().x())+tr(" ")+QString::number(mouseEvent->pos().y()));
        char buf[64];
        sprintf(buf,"mouseClick %d %d", mouseEvent->pos().x(), mouseEvent->pos().y()-startX);
        send_str(buf);
        drawPoint(mouseEvent->pos().x(), mouseEvent->pos().y());
        break;
    }
    case QEvent::MouseButtonRelease:{
        QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event);
        ui->show2->setText(tr("MouseButtonRelease:")+QString::number(mouseEvent->pos().x())+tr(" ")+QString::number(mouseEvent->pos().y()));
        char buf[64];
        sprintf(buf,"mouseRelease %d %d", mouseEvent->pos().x(), mouseEvent->pos().y()-startX);
        send_str(buf);
        break;
    }
    case QEvent::MouseButtonDblClick:{
        QMouseEvent * mouseEvent = static_cast<QMouseEvent *>(event);
        ui->show3->setText(tr("MouseButtonDblClick:")+QString::number(mouseEvent->pos().x())+tr(" ")+QString::number(mouseEvent->pos().y()));
        char buf[64];
        sprintf(buf,"mouseDClick %d %d", mouseEvent->pos().x(), mouseEvent->pos().y()-startX);
        send_str(buf);
        drawPoint(mouseEvent->pos().x(), mouseEvent->pos().y());
        break;
    }
    default:
        break;
    }

    return QWidget::event(event);
}

void MainWindow::send_str(char sendBuf[])
{
    if(!isOpen)
        return ;
    int send_len = 0;
    int temp=0;
    while(temp++<10)
    {
        send_len = tcpSocket->write(sendBuf, 64);
        if (send_len < 0) {
            qDebug() <<"try times "<<temp<< ": 发送失败！" << endl;
        }
        else
            break;
    }
}

void MainWindow::on_btn_clicked()
{

}

void MainWindow::on_btn_2_clicked()
{
    canPress=!canPress;
    if(canPress)
    {
        ui->btn->setVisible(true);
        ui->labelCur->setVisible(false);
        ui->btn_2->setText(tr("P"));
    }
    else
    {
        ui->btn->setVisible(false);
        ui->labelCur->setVisible(true);
        ui->btn_2->setText(tr("M"));
    }
}

void MainWindow::on_btn_3_clicked()
{
    showNet=!showNet;
    if(showNet)
    {
        ui->btn_3->setText("N");
        showLabel(netStep);
    }
    else
    {
        ui->btn_3->setText("n");
        for(int showed=0;showed<net.size();showed++)
        {
            QLabel *label = net[showed];
            label->setVisible(false);
        }
    }
}

void MainWindow::on_btn_4_clicked()//net up
{
    if(showNet)
    {
        if(netStep<200)
        {
            netStep+=5;
            showLabel(netStep);
        }
    }
}


void MainWindow::on_netDown_clicked()
{
    if(showNet)
    {
        if(netStep>35)
        {
            netStep-=5;
            showLabel(netStep);
        }
    }
}
